package com.yanqu.road.server.cmd.cross.player;

import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.interactive.InteractiveRecord;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.bussiness.player.UserAttributeBussiness;
import com.yanqu.road.logic.bussiness.player.UserBussiness;
import com.yanqu.road.logic.bussiness.player.UserInteractiveBussiness;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.base.AbstractServerCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.EarnSpeedModule;
import com.yanqu.road.server.gameplayer.module.player.InteractiveModule;
import com.yanqu.road.server.manger.config.InteractiveMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.rank.RankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;

@Cmd(code = Protocol.S_CROSS_GIVE_LIKE, desc = "跨服点赞玩家")
public class CrossGiveLikePlayerCmd extends AbstractServerCommand {

	private long userId;
	private UserBaseInfo userBaseInfo;
	private int type;
	private long likeUserId;
	private boolean isPreReduce;

	private int heartCount;
	private UserInfo likeUserInfo;
	private BigInteger effectValue = BigInteger.ZERO;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		PlayerProto.CrossGiveLikeReqMsg reqMsg = PlayerProto.CrossGiveLikeReqMsg.parseFrom(packet.getBytes());
		userId = reqMsg.getUserId();
		userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getPlayerBaseData());
		type = reqMsg.getType();
		likeUserId = reqMsg.getLikeUserId();
		isPreReduce = reqMsg.getIsPreReduce();
		return true;
	}

	@Override
	public void execute(YanQuMessage packet) throws Exception {
		likeUserInfo = UserMgr.getUserInfo(likeUserId);
		if(null == likeUserInfo){
			ret = GameErrorCode.E_USER_NO_FOUND;
			return;
		}

		GamePlayer likePlayer = GamePlayerMgr.getOnlinePlayer(likeUserId);
		int limitValue = InteractiveMgr.getBeLikeLimit(likeUserId);
		synchronized (likeUserInfo){
			heartCount = likeUserInfo.getHeartCount();
			if(heartCount >= limitValue){
				ret = GameErrorCode.E_PLAYER_CURRENT_LIKE_LIMIT;
				return;
			}
			heartCount++;
			likeUserInfo.setHeartCount(heartCount);
			effectValue = UserMgr.getHeartEffectEarnSpeed(likeUserInfo.getBaseEarnSpeed());

			if(null != likePlayer){
				likePlayer.getModule(AttributeModule.class).syncAttribute(ePlayerAttrType.HeartCount);
				//likePlayer.getModule(EarnSpeedModule.class).scheduleAddSilver();
			}else {
				likeUserInfo.setEarnSpeed(likeUserInfo.getEarnSpeed().add(effectValue));
				//赚速榜
				RankMgr.changeUserRank(eBigRankType.Normal.getValue(), likeUserInfo.getUserId(), likeUserInfo.getEarnSpeed(), eRankType.EarnSpeed.getValue(), "");
			}
		}
		if(null != likePlayer) {
			//likePlayer.getModule(AttributeModule.class).syncAttribute(ePlayerAttrType.HeartCount);
			likePlayer.getModule(EarnSpeedModule.class).scheduleAddSilver();
		}
			if(1 == heartCount){
			likeUserInfo.setLastHeartRecoverTime(System.currentTimeMillis() / 1000);
			if(null == likePlayer){
				UserBussiness.updateLastHeartRecoverTimeAndEarnSpeed(likeUserId, heartCount, likeUserInfo.getLastHeartRecoverTime(), likeUserInfo.getEarnSpeed());
			}
		}else {
			if(null == likePlayer){
				UserBussiness.updateHeartCountAndEarnSpeed(likeUserId, heartCount, likeUserInfo.getEarnSpeed());
			}
		}
		if(null == likePlayer){
			UserAttributeBussiness.updateAddUserAttribute(likeUserId, ePlayerAttrType.TotalBeGiveLikeTimes, 1);
		}else {
			AttributeModule likeAttributeModule = likePlayer.getModule(AttributeModule.class);
			likeAttributeModule.addAttribute(ePlayerAttrType.TotalBeGiveLikeTimes, 1);
			likePlayer.notifyListener(eGamePlayerEventType.TotalBeGiveLikeTimes.getValue(), likeAttributeModule.getAttribute(ePlayerAttrType.TotalBeGiveLikeTimes));
		}

		InteractiveRecord interactiveRecord = UserMgr.initInteractiveRecord(likeUserId, eInteractiveType.giveLike.getValue(), userId, userBaseInfo);
		if(null != likePlayer) {
			likePlayer.getModule(InteractiveModule.class).addInteractiveRecord(interactiveRecord, effectValue);
		}else {
			UserInteractiveBussiness.addInteractiveRecord(interactiveRecord);
		}
	}

	@Override
	public void sendData(YanQuMessage packet) throws Exception {
		PlayerProto.CrossGiveLikeRespMsg.Builder respMsg = PlayerProto.CrossGiveLikeRespMsg.newBuilder();
		respMsg.setUserId(userId);
		respMsg.setServerId(userBaseInfo.getServerId());
		respMsg.setType(type);
		respMsg.setLikeUserId(likeUserId);
		respMsg.setIsPreReduce(isPreReduce);
		respMsg.setRet(ret);
		if(0 == ret){
			respMsg.setHeartCount(heartCount);
			respMsg.setEffectValue(effectValue.toString());
			respMsg.setEarnSpeed(likeUserInfo.getEarnSpeed().toString());
		}
		YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_GIVE_LIKE_RESP, respMsg);
		GamePlayerMgr.sendPacket(0, pbMsg);
	}
}
